import { getDistance, isInArray, unzipPosition } from "@/utils"

/* 处理其他任务的爬虫扩展 */
export default class CreepAction extends Creep {
    /* 处理房间签名任务 */
    public HandleSG():void{
        if (!this.memory.bindTask) return
        var thisTask = Game.rooms[this.memory.belong].getMisson_ID(this.memory.bindTask)
        if (this.room.name != thisTask.disRoom[0])
        {
            this.goTo(new RoomPosition(25,25,thisTask.disRoom[0]),20)
        }
        else
        {
            if (this.signController(this.room.controller,thisTask.words) == ERR_NOT_IN_RANGE)
            {
                this.goTo(this.room.controller.pos,1)
            }
            else if (this.signController(this.room.controller,thisTask.words) == OK)
            {
                Game.rooms[this.memory.belong].deleteMisson_ID(this.memory.bindTask)
                this.suicide()
            }
        }
    }

    /* 处理原矿开采任务 */
    public HandleRMH():void{
        var thisTask = Game.rooms[this.memory.belong].getMisson('CREEP','原矿开采')
        this.workstate(thisTask.relateResource[0])
        var extract_ = Game.getObjectById(this.room.memory.TaskMemory.extractID as string) as StructureExtractor
        var mineral_ = Game.getObjectById(this.room.memory.TaskMemory.mineralID as string) as Mineral
        if (!mineral_ || !extract_) return
        if (!this.memory.ContainerID)
        {
            var containers = mineral_.pos.getRangedStructure([STRUCTURE_CONTAINER],1,0) as StructureContainer[]
            if (containers.length <= 0) return
            this.memory.ContainerID = containers[0].id
        }
        if (!this.memory.StorageId)
        {
            
            this.memory.StorageId = this.room.memory.TaskMemory.storageID as string
        }
        var container_ = Game.getObjectById(this.memory.ContainerID) as StructureContainer
        if (!container_) {delete this.memory.ContainerID;return}
        /* 工作部分 */
        if (this.memory.working)
        {
            var storage_ = Game.getObjectById(this.room.memory.TaskMemory.storageID as string) as StructureStorage
            if (!storage_) return
            if (this.ticksToLive < 20 && this.pos.isEqualTo(container_))
                this.suicide()
            else
                this.transfer__(storage_,thisTask.relateResource[0])
        }
        else
        {
            if (!this.pos.isEqualTo(container_.pos)) this.moveTo(container_,{reusePath:20})
            else
            {
                if (!extract_.cooldown)
                    this.harvest(mineral_)
                if (Game.time % 100 && container_.store[thisTask.relateResource[0]] > this.store.getCapacity())
                    this.withdraw(container_,thisTask.relateResource[0])
            }
        }
    }

    /* 处理外矿开采任务 */
    public HandleOMH():void{
        if (!this.memory.bindTask) return
        var thisTask = Game.rooms[this.memory.belong].getMisson_ID(this.memory.bindTask)
        if (!thisTask) {delete this.memory.bindTask;return}
        if (!this.memory.disRoom) this.memory.disRoom = thisTask.disRoom[0]
        /* 没有外矿数据就无脑跑去外矿房间获取外矿数据 */
        this.workstate(RESOURCE_ENERGY)
        /* 爬虫血量减少就注意周围是否有敌虫，如果有就进入战斗状态 */
        if (this.hits < this.hitsMax && this.room.name != this.memory.belong )
        {
                
                var enemyCreeps = this.room.find(FIND_HOSTILE_CREEPS,{filter:(creep)=>{
                    return !isInArray(Memory.whitesheets,creep.owner.username)
                }})
                if (enemyCreeps.length > 0)
                {
                    // 这个时候就进入战斗状态，开始出战斗爬虫
                    thisTask.state = 2
                }
                if (this.pos.LookConfirm('road') && thisTask.state == 2 && this.room.name != this.memory.belong)
                {
                    /* 避开道路，防止道路被摧毁 寻找周围1格内没有建筑的平原，并移动到那里 */
                    var posList = []
                    for (var ix of [-1,0,1])
                        for (var jy of [-1,0,1])
                        {
                            var pos = new RoomPosition(this.pos.x+ix,this.pos.y+jy,thisTask.disRoom[0])
                            if (!pos.LookConfirm('road')) posList.push(pos)
                        }
                    if (posList.length > 0)
                    {
                        this.moveTo(posList[0])
                    }
                    
                }
        }
        if (!thisTask.OutMineData || !Game.rooms[this.memory.disRoom])
        {
            this.farMoveTo(new RoomPosition(25,25,this.memory.disRoom),20)
            return
        }
        else
        {
            if (this.memory.role == 'out-claim')
            {
                var disPos = unzipPosition(thisTask.OutMineData['control'])
                if (!this.pos.isNearTo(disPos)) this.farMoveTo(disPos,1)
                else{
                    if (!this.memory.num) this.memory.num = 5000
                    if (this.room.controller.reservation && this.room.controller.reservation.ticksToEnd && this.room.controller.reservation.username == this.owner.username && this.room.controller.reservation.ticksToEnd <= this.memory.num)
                    {
                    var Cores = this.room.find(FIND_STRUCTURES,{filter:(structure)=>{
                        return structure.structureType == STRUCTURE_INVADER_CORE
                    }})
                    if (Cores.length > 0)
                        thisTask.state = 2
                    }
                    this.reserveController(this.room.controller)
                    if (this.room.controller.reservation && this.room.controller.reservation.ticksToEnd)
                    this.memory.num = this.room.controller.reservation.ticksToEnd
                }
            }
            else if (this.memory.role == 'out-min')
            {
                
                if (!this.memory.disPos)
                {
                    LoopSP:
                    for (var sp of thisTask.OutMineData['source'])
                    {
                        for (var c of thisTask.bindData['out-min'].bind)
                        {
                            if (Game.creeps[c] && Game.creeps[c].memory.disPos == sp)
                            continue LoopSP

                        }
                        this.memory.disPos = sp
                    }
                    return
                }
                else
                {
                    var disSource = unzipPosition(this.memory.disPos).lookFor(LOOK_SOURCES)[0]
                        if (!disSource) return
                    if (!this.pos.isNearTo(disSource))
                        this.farMoveTo(disSource.pos,1)
                    else
                    {
                        if (!this.memory.working)
                        {
                            this.harvest(disSource)
                            return
                        }
                        var container_ = disSource.pos.findInRange(FIND_STRUCTURES,1,{filter:{structureType:STRUCTURE_CONTAINER}})
                        if (container_.length <= 0 )
                        {
                            this.pos.createConstructionSite(STRUCTURE_CONTAINER)
                            if (this.memory.working)
                            {
                                var cons_ = this.pos.findInRange(FIND_CONSTRUCTION_SITES,1,{filter:{structureType:STRUCTURE_CONTAINER}})
                                if (cons_.length > 0)
                                this.build(cons_[0])
                                return
                            }
                        }
                        else
                        {
                            if (Game.time % 10)
                            if (!this.pos.isEqualTo(container_[0]))
                            {
                                this.moveTo(container_[0])
                            }
                            if (container_[0].hits < container_[0].hitsMax)
                            {
                                this.repair(container_[0])
                                return
                            }
                            this.transfer(container_[0],RESOURCE_ENERGY)
                        }
                    }
                }
            }
            else if (this.memory.role == 'out-car')
            {
                if (!this.memory.disPos)
                {
                    LoopSP:
                    for (var sp of thisTask.OutMineData['container'])
                    {
                        for (var c of thisTask.bindData['out-car'].bind)
                        {
                            if (Game.creeps[c] && Game.creeps[c].memory.disPos == sp)
                            continue LoopSP
                        }
                        this.memory.disPos = sp
                    }
                    return
                }
                else
                {
                    var confirm = unzipPosition(this.memory.disPos).LookConfirm(STRUCTURE_CONTAINER)
                    if (!confirm && Game.rooms[thisTask.disRoom[0]])
                    {
                        /* 没有就删除数据 */
                        for (var i of thisTask.OutMineData['container'])
                        {
                            if (i == this.memory.disPos)
                            {
                                var index = thisTask.OutMineData['container'].indexOf(i)
                                thisTask.OutMineData['container'].splice(index,1)
                            }
                            delete this.memory.disPos
                        }
                        delete this.memory.disPos
                        return
                    }
                    var DisStructure = unzipPosition(this.memory.disPos).lookFor(LOOK_STRUCTURES)
                    var DisContainer:StructureContainer
                    for (var cc of DisStructure) if (cc.structureType == 'container')DisContainer = cc as StructureContainer
                    if (!DisContainer) return
                    if (this.memory.working)
                    {
                        if (Game.time % 2 == 0)
                            this.say('🚬',true)
                        var con_ = this.pos.findClosestByPath(FIND_CONSTRUCTION_SITES)
                        if (con_)
                        {
                            if (this.build(con_) == ERR_NOT_IN_RANGE)
                                this.moveTo(con_)
                            return
                        }
                        if (!Game.rooms[this.memory.belong].memory.TaskMemory.storageID) return
                        var storage_ = Game.getObjectById(Game.rooms[this.memory.belong].memory.TaskMemory.storageID as string) as StructureStorage
                        if (!storage_) return
                        if (!this.pos.isNearTo(storage_))
                        {
                            /* 寻找脚下的路，如果Hits低于最高，就修复 */
                            var structures = this.pos.lookFor(LOOK_STRUCTURES)
                            if (structures.length > 0)
                            // var roadConfirm = false
                            for (var st of structures)
                            {
                                if (st.structureType == 'road')
                                {
                                if (st.hits < st.hitsMax)
                                {this.repair(st);return}
                                // roadConfirm = true
                                }
                            }
                            // var Rcon_ = this.pos.lookFor(LOOK_CONSTRUCTION_SITES)
                            // if (!roadConfirm && Rcon_.length <= 0 && !isInArray([0,49],this.pos.x) && !isInArray([0,49],this.pos.y))
                            // {
                            //     this.pos.createConstructionSite('road')
                            //     return
                            // }
                            this.farMoveTo(storage_.pos,1)
                        }
                        else
                        {
                            this.transfer(storage_,'energy')
                        }
                    }
                    else
                    {
                        if (this.ticksToLive < 130) this.suicide()
                        if (!this.pos.isNearTo(DisContainer))
                            this.farMoveTo(DisContainer.pos,0)
                        else
                            this.withdraw(DisContainer,'energy')
                    }
                    
                }
                /*  */
            }
            else if (this.memory.role == 'out-defend')
            {
                if (this.room.name != this.memory.disRoom)
                    this.farMoveTo(new RoomPosition(25,25,this.memory.disRoom),20)
                else
                {
                    /* 进房间后处理逻辑 */
                    var enemys = this.pos.findClosestByPath(FIND_HOSTILE_CREEPS,{filter:(creep)=>{
                        return !isInArray(Memory.whitesheets,creep.owner.username)
                    }})
                    var cores = this.pos.findClosestByPath(FIND_STRUCTURES,{filter:(structure)=>{
                        return structure.structureType == STRUCTURE_INVADER_CORE
                    }})
                    if (enemys)
                    {
                        this.say("💢",true)
                        this.heal(this)
                        if (this.rangedAttack(enemys) == ERR_NOT_IN_RANGE)
                        {
                            if (this.pos.x == 0 || this.pos.y == 0 || this.pos.x == 49 || this.pos.y == 49)
                        {
                            
                            this.moveTo(new RoomPosition(25,25,this.room.name))
                        }
                        else
                            this.moveTo(enemys.pos)
                        }
                        return
                    }
                    if (cores)
                    {
                        this.say("💢",true)
                        this.heal(this)
                        if (this.rangedAttack(cores) == ERR_NOT_IN_RANGE)
                        {
                            if (this.pos.x == 0 || this.pos.y == 0 || this.pos.x == 49 || this.pos.y == 49)
                        {
                            
                            this.moveTo(new RoomPosition(25,25,this.room.name))
                        }
                        else
                            this.moveTo(cores.pos)
                        }
                        return
                    }
                    var injuredCreep = this.pos.findClosestByRange(FIND_MY_CREEPS,{filter:(creep)=>{
                        return creep.hits < creep.hitsMax
                    }})
                    if (injuredCreep)
                    {
                        if (this.heal(injuredCreep) == ERR_NOT_IN_RANGE)
                        {
                            this.moveTo(injuredCreep)
                            return
                        }   
                    }
                    
                }
            }
        }
        
    }

    /* 处理控制冲级任务 */
    public HandleCIL():void{
        var thisTask = this.room.getMisson("CREEP","控制冲级")
        this.workstate('energy')
        if (thisTask.switch == true && !this.memory.boosted)
        {
            var disLab:StructureLab
            if (Object.keys(thisTask.LabBind).length == 1)
            return
                // disLab = Game.getObjectById(thisTask.LabBind[Object.keys(thisTask.LabBind)[0]][0])
            else
                return
            if (disLab.boostCreep(this) == ERR_NOT_IN_RANGE)
            {
                this.rush(disLab.pos)
            }
            else if (disLab.boostCreep(this) == OK)
            {
                this.memory.boosted = true
            }
            return
        }  
        if (this.memory.working)
        {
            this.upgrade__()
        }
        else
        {
            var ULID = this.room.memory.TaskMemory.upgrade_link as string
            if (!ULID) return
            var upgrade_link = Game.getObjectById(ULID as string) as StructureLink
            if (!upgrade_link) {this.room.deleteMisson("CREEP","控制冲级");return}
            this.withdraw__(upgrade_link,'energy')
        }

    }

    /* 处理刷墙任务 */
    public HandleCRW():void{
        if (!this.memory.bindTask) return
        var thisTask = Game.rooms[this.memory.belong].getMisson_ID(this.memory.bindTask) 
        if (thisTask.switch && !this.memory.boosted)
        {
            this.say("哈")
            return
        }
        this.workstate('energy')
        if (thisTask.type == '全局刷墙')
        {
            if (this.memory.working)
            {
                if (this.memory.targetId)
                {
                    var target_ = Game.getObjectById(this.memory.targetId) as StructureRampart
                    if (!target_) delete this.memory.targetId
                    this.repair__(target_)
                }
                else this.memory.working = false
            }
            else
            {
                /* 寻找hits最小的墙 */
                var leastRam = this.room.getListHitsleast([STRUCTURE_RAMPART,STRUCTURE_WALL],3)
                if (!leastRam) return
                this.memory.targetId = leastRam.id
                var storage_ = Game.getObjectById(Game.rooms[this.memory.belong].memory.TaskMemory.storageID as string) as StructureStorage
                if (!storage_){Game.rooms[this.memory.belong].deleteMisson_ID(thisTask.ID);return}
                this.withdraw__(storage_,'energy')
            }
        }
        else if (thisTask.type == '针对刷墙'){
            if (this.memory.working)
            {
                if (this.memory.targetId)
                {
                    var target_ = Game.getObjectById(this.memory.targetId) as StructureRampart
                    if (!target_) delete this.memory.targetId
                    this.repair__(target_)
                }
                else this.memory.working = false
            }
            else
            {
                /* 寻找所有白黄旗，并获取旗下的建筑id */
                var allFlag = this.room.find(FIND_FLAGS,{filter:(flag)=>{
                    return flag.color == COLOR_WHITE &&flag.secondaryColor == COLOR_YELLOW
                }})
                var storage_ = Game.getObjectById(Game.rooms[this.memory.belong].memory.TaskMemory.storageID as string) as StructureStorage
                if (!storage_ || allFlag.length <= 0){Game.rooms[this.memory.belong].deleteMisson_ID(thisTask.ID);return}
                var disStructure:StructureWall | StructureRampart
                for (var i of allFlag)
                {
                    var thisrampart = i.pos.GetStructure('rampart') as StructureRampart
                    var thiswall = i.pos.GetStructure("constructedWall") as StructureWall
                    let tempStructure = thisrampart?thisrampart:thiswall
                    if (!disStructure || disStructure.hits > tempStructure.hits)
                        disStructure = tempStructure
                }
                if (!disStructure) return
                this.memory.targetId = disStructure.id
                this.withdraw__(storage_,'energy')
            }
        }
    }
    /* 处理强化刷墙任务 */
    public HandleBRW():void{
        if (!this.memory.bindTask) return
        var thisTask = Game.rooms[this.memory.belong].getMisson_ID(this.memory.bindTask)

        if (!thisTask) {delete this.memory.bindTask;return}
        // boost
        if (!this.memory.boosted)
        {
            var t = Object.keys(thisTask.LabBind)
            var list = {
                'work':thisTask.LabBind[t[0]][1],
            }
            for (var b of this.body)
            {
                if (!isInArray(['work'],b.type))
                    continue
                if (!b.boost)
                {
                    var rType = b.type
                    for (var i in thisTask.LabBind)
                    {
                        if (thisTask.LabBind[i][1] == list[rType])
                        {
                            var disLab = Game.getObjectById(thisTask.LabBind[i][0]) as StructureLab
                            if (!disLab) return
                            if (!this.pos.isNearTo(disLab))
                            {
                                this.rush(disLab.pos)
                            }
                            else
                            {
                                disLab.boostCreep(this)
                            }
                            return
                        }
                    }
                    return
                }
            }
            this.memory.boosted = true
        }
        // 区分工作性质
        this.workstate('energy')
        if (thisTask.type == '全局刷墙')
        {
            if (this.memory.working)
            {
                if (this.memory.targetId)
                {
                    var target_ = Game.getObjectById(this.memory.targetId) as StructureRampart
                    if (!target_) delete this.memory.targetId
                    this.repair_(target_)
                }
                else this.memory.working = false
            }
            else
            {
                /* 寻找hits最小的墙 */
                var leastRam = this.room.getListHitsleast([STRUCTURE_RAMPART,STRUCTURE_WALL],3)
                if (!leastRam) return
                this.memory.targetId = leastRam.id
                var storage_ = Game.getObjectById(Game.rooms[this.memory.belong].memory.TaskMemory.storageID as string) as StructureStorage
                if (!storage_){Game.rooms[this.memory.belong].deleteMisson_ID(thisTask.ID);return}
                this.withdraw__(storage_,'energy')
            }
        }
        else if (thisTask.type == '针对刷墙')
        {
            if (this.memory.working)
            {
                if (this.memory.targetId)
                {
                    var target_ = Game.getObjectById(this.memory.targetId) as StructureRampart
                    if (!target_) delete this.memory.targetId
                    this.repair_(target_)
                    
                    if (Game.flags[`${this.memory.belong}/R`] && getDistance(target_.pos,Game.flags[`${this.memory.belong}/R`].pos) <= 3)
                    this.moveTo(Game.flags[`${this.memory.belong}/R`])
                }
                else this.memory.working = false
            }
            else
            {
                /* 寻找所有白黄旗，并获取旗下的建筑id */
                var allFlag = this.room.find(FIND_FLAGS,{filter:(flag)=>{
                    return flag.color == COLOR_WHITE &&flag.secondaryColor == COLOR_YELLOW
                }})
                var storage_ = Game.getObjectById(Game.rooms[this.memory.belong].memory.TaskMemory.storageID as string) as StructureStorage
                if (!storage_ || allFlag.length <= 0){Game.rooms[this.memory.belong].deleteMisson_ID(thisTask.ID);return}
                var disStructure:StructureWall | StructureRampart
                for (var ii of allFlag)
                {
                    var thisrampart = ii.pos.GetStructure('rampart') as StructureRampart
                    var thiswall = ii.pos.GetStructure("constructedWall") as StructureWall
                    let tempStructure = thisrampart?thisrampart:thiswall
                    if (!disStructure || disStructure.hits > tempStructure.hits)
                        disStructure = tempStructure
                }
                if (!disStructure) return
                this.memory.targetId = disStructure.id
                this.withdraw__(storage_,'energy')
            }
        }
    }
    /* 处理扩张援建任务 */
    public HandleEHB():void{
        if (!this.memory.bindTask) return
        var thisTask = Game.rooms[this.memory.belong].getMisson_ID(this.memory.bindTask)
        if (this.getActiveBodyparts('heal')>0) this.heal(this)
        if (thisTask.switch && !this.memory.boosted)
        {
            this.say("哈")
            return
        }
        this.workstate('energy')
        if (this.room.name != thisTask.disRoom[0])
        {
            this.farMoveTo(new RoomPosition(25,25,thisTask.disRoom[0]),20)
            if (this.hits < this.hitsMax)
            {
                
            }
        }
        else
        {
            if (this.memory.role == 'e-claimer')
            {
                if (this.room.controller && !this.room.controller.my)
                {
                    if (this.claimController(this.room.controller) == ERR_NOT_IN_RANGE)
                        this.moveTo(this.room.controller)
                    return
                }
                var injuredCreep = this.pos.findClosestByPath(FIND_MY_CREEPS,{filter:(creep)=>{
                    return creep.hits < creep.hitsMax
                }})
                if (injuredCreep && this.heal(injuredCreep) == ERR_NOT_IN_RANGE)
                {
                    this.moveTo(injuredCreep)
                }
            }
            else if (this.memory.role == 'e-upgrader')
            {
                if (this.memory.working)
                {
                    this.upgrade_()
                }
                else
                {
                    if (!this.memory.targetId)
                    this.memory.targetId = this.pos.findClosestByPath(FIND_SOURCES).id
                    else
                    {
                        var source_ = Game.getObjectById(this.memory.targetId) as Source
                        this.harvest__(source_)
                    }
                }
            }
            else if (this.memory.role == 'e-builder')
            {
                if (this.memory.working)
                {
                    var con_ = this.pos.findClosestByPath(FIND_CONSTRUCTION_SITES)
                    if (con_){this.build_(con_);return}
                    var extension_ = this.pos.getClosestStore()
                    this.transfer_(extension_,'energy')
                }
                else
                {
                    /* 寻找有无大于500的掉落资源或者storage_ */ 
                    var storage_ = this.pos.getClosestStructure(['storage'],0) as StructureStorage
                    if (storage_ && storage_.store['energy'] > 600)
                    {
                        this.withdraw__(storage_,'energy')
                        return
                    }
                    var drop_ = this.pos.findClosestByPath(FIND_DROPPED_RESOURCES)
                    if (drop_ && drop_.amount > 500)
                    {
                        if (this.pickup(drop_) == ERR_NOT_IN_RANGE)
                        { this.moveTo(drop_);return}
                    }
                    if (!this.memory.targetId)
                    this.memory.targetId = this.pos.findClosestByPath(FIND_SOURCES).id
                    else
                    {
                        var source_ = Game.getObjectById(this.memory.targetId) as Source
                        this.harvest__(source_)
                    }
                }
            }
        }
    }

    /* 处理资源掠夺任务 */
    public HandleRP():void{
        if (!this.memory.bindTask) return
        var thisTask = Game.rooms[this.memory.belong].getMisson_ID(this.memory.bindTask)
        if (thisTask.switch && !this.memory.boosted)
        {
            this.say("哈")
            return
        }var disRoom = thisTask.disRoom[0]
        /* 工作状态 */
        var store_ = this.store
        var allStoreKeys = Object.keys(store_)
        if (!this.memory.working) this.memory.working = false
        if (this.memory.working && allStoreKeys.length <= 0)
        {
            this.memory.working = false
        }
        if (!this.memory.working && this.store.getFreeCapacity() == 0)
        {
            this.memory.working = true
        }
        if (this.memory.working)
        {
            if (this.room.name != this.memory.belong)
            {
                this.farMoveTo(new RoomPosition(25,25,this.memory.belong),20)
            }
            else
            {
                var storage_ = this.pos.getClosestStructure([STRUCTURE_STORAGE,STRUCTURE_CONTAINER],0) as StructureStorage
                if (!storage_) return
                var st = this.store
                for (var i of Object.keys(st))
                {
                    this.transfer__(storage_,i as ResourceConstant)
                }

            }
        }
        else
        {
            if (thisTask.state == 1)
            {
                if (this.room.name != disRoom)
                {
                    if (this.room.name == this.memory.belong && this.store.getUsedCapacity() == 0)
                    {
                        if (this.ticksToLive < thisTask.num)
                            this.suicide()
                    }
                this.farMoveTo(new RoomPosition(25,25,disRoom),20)
                }
                else
                {
                var tanks = this.room.find(FIND_STRUCTURES,{filter:(structure)=>{
                    return (structure.structureType == STRUCTURE_STORAGE) && Object.keys(structure.store).length > 0
                }}) as StructureStorage[]
                if (tanks.length > 0)
                {
                    var stores = tanks[0].store
                    var dis_resource = Object.keys(stores)[0]
                    for (var i in stores)
                    {
                        if (tanks[0].store.getUsedCapacity(i as ResourceConstant) < tanks[0].store.getUsedCapacity(dis_resource as ResourceConstant))
                        dis_resource = i
                    }
                    this.withdraw_(tanks[0],dis_resource as ResourceConstant)
                    if (tanks[0].store.getUsedCapacity() <= 500  )
                    {
                        thisTask.state = 2
                    }
                }
                else
                {
                    // 寻找凋落的资源
                    // const target = this.pos.findClosestByRange(FIND_DROPPED_RESOURCES)
                    // if(target) {
                    // if(this.pickup(target) == ERR_NOT_IN_RANGE) {
                    //     this.moveTo(target)
                    //     }
                    // }
                    // 寻找旗子下的遗迹
                    if (Game.flags[`${this.memory.belong}/LD`])
                    {
                        var LDFlag = Game.flags[`${this.memory.belong}/LD`]
                        var ruin = LDFlag.pos.lookFor(LOOK_RUINS)
                        if (ruin.length > 0)
                        {
                            var store__ = ruin[0].store
                            var dis_resource = Object.keys(store__)[0]
                            for (var i in store__)
                            {
                                if (store__.getUsedCapacity(i as ResourceConstant) < store__.getUsedCapacity(dis_resource as ResourceConstant))
                                dis_resource = i
                            }
                            //this.withdraw_(ruin[0],dis_resource as ResourceConstant)
                            if (this.withdraw(ruin[0] as Ruin,dis_resource as ResourceConstant) == ERR_NOT_IN_RANGE)
                                this.moveTo(ruin[0])
                            if (ruin[0].store.getUsedCapacity() <= 500  )
                            {
                                thisTask.state = 2
                            }
                        }
                    }
                }
                }
            }
            else if (thisTask.state == 2)
            {
                this.suicide()
            }
            
        }
    }


}